package com.palfund.ui.recyclerview;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.palfund.ui.R;
import com.palfund.ui.recyclerview.adapter.GridAdapter;
import com.palfund.ui.recyclerview.adapter.RecyclerViewAdapter;

import java.util.ArrayList;
import java.util.HashMap;

public class GridActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setHasFixedSize(true);
        GridAdapter adapter = new GridAdapter(this);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new RecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, Object itemData) {
                Toast.makeText(GridActivity.this, "Item click" + view.getId(), Toast
                        .LENGTH_SHORT).show();
            }
        });
        adapter.setOnItemLongClickListener(new RecyclerViewAdapter.OnItemLongClickListener() {
            @Override
            public void onItemLongClick(View view, int position, Object itemData) {
                Toast.makeText(GridActivity.this, "Item LongClick" + view.getId(), Toast
                        .LENGTH_SHORT).show();
            }
        });
        final int[] images = {R.mipmap.star, R.mipmap.palace};
        ArrayList<HashMap<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("index", "id   " + i);
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), images[i % 2]);
            hashMap.put("image", bitmap);
            list.add(hashMap);
        }
        adapter.insertItemRange(0, list);
    }
}
